package com.ouyy.volunteerservice.volunteer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ouyy.volunteerservice.volunteer.domain.po.Evaluation;
import com.ouyy.volunteerservice.volunteer.mapper.EvaluationMapper;
import com.ouyy.volunteerservice.volunteer.service.EvaluationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation>
        implements EvaluationService {

    @Autowired
    private EvaluationMapper evaluationMapper;
    @Override
    public boolean updateScoreByOrganizationIdAndVolunteerId(String organationId, String volunterId, Double score) {
        // 构造更新条件
        UpdateWrapper<Evaluation> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("organization_id", organationId)
                .eq("volunteer_id", volunterId)
                .set("score", score);
        // 执行更新
        return evaluationMapper.update(null, updateWrapper) > 0;
    }


    @Override
    public String queryByorganationId(String organizationId, String volunterId) {
        QueryWrapper<Evaluation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("organization_id", organizationId)
                .eq("volunteer_id", volunterId)
                .select("score");

        List<String> scores = this.list(queryWrapper).stream()
                .map(e -> String.valueOf(e.getScore()))
                .collect(Collectors.toList());
        return String.join(", ", scores);
    }

}
